#include "PngImage.h"
#include "Utils.h"
#include "libpng/png.h"
#include "Image.h"

#ifdef ANDROID

#include "libzip/zip.h"
#include "ApkLoader.h"

zip_file* file;

void png_zip_read(png_structp png_ptr, png_bytep data, png_size_t length) {
	zip_fread(file, data, length);
}

#endif

PngImage::PngImage(){};

#ifdef WIN32

PngImage::PngImage(const char* fileName)
{
	FILE* f;
	f = fopen(fileName, "rb");

	//header for testing if it is a png
	png_byte header[8];

	//read the header
	fread(header, 8, sizeof(char), f);

	//test if png
	int is_png = !png_sig_cmp(header, 0, 8);
	if (!is_png) {
		fclose(f);
		PRINT("Not is png");
	}

	//create png struct
	png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
		NULL, NULL);
	if (!png_ptr) {
		fclose(f);
		PRINT("Cannot create png struct");
	}

	//create png info struct
	png_infop info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr) {
		png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
		fclose(f);
		PRINT("Cannot create png info struct");
	}

	//create png info struct
	png_infop end_info = png_create_info_struct(png_ptr);
	if (!end_info) {
		png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
		fclose(f);
		PRINT("Cannot create png info struct");
	}

	//png error stuff, not sure libpng man suggests this.
	if (setjmp(png_jmpbuf(png_ptr))) {
		fclose(f);
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		PRINT("Cannot setjump");
	}

	//init png reading
	png_init_io(png_ptr, f);

	//let libpng know you already read the first 8 bytes
	png_set_sig_bytes(png_ptr, 8);

	// read all the info up to the image data
	png_read_info(png_ptr, info_ptr);

	//variables to pass to get info
	int bit_depth, color_type;
	png_uint_32 twidth, theight;

	// get info about png
	png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
		NULL, NULL, NULL);

	//update width and height based on png info
	width = twidth;
	height = theight;

	// Update the png info struct.
	png_read_update_info(png_ptr, info_ptr);

	// Row size in bytes.
	int rowbytes = png_get_rowbytes(png_ptr, info_ptr);

	// Allocate the imageData as a big block, to be given to opengl
	//png_byte *imageData = new png_byte[rowbytes * height];
	imageData = new png_byte[rowbytes * height];
	if (!imageData) {
		//clean up memory and close stuff
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		fclose(f);
		PRINT("Cannot create memory for image data");
	}

	//row_pointers is for pointing to imageData for reading the png with libpng
	png_bytep *row_pointers = new png_bytep[height];
	if (!row_pointers) {
		//clean up memory and close stuff
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		delete[] imageData;
		fclose(f);
		PRINT("Cannot create memory for row pointer");
	}
	// set the individual row_pointers to point at the correct offsets of imageData
	for (int i = 0; i < height; ++i)
		row_pointers[height - 1 - i] = imageData + i * rowbytes;

	//read the png into imageData through row_pointers
	png_read_image(png_ptr, row_pointers);

	png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
	delete[] row_pointers;
	fclose(f);
}

#elif ANDROID

PngImage::PngImage(const char* fileName)
{
	file = zip_fopen(ApkLoader::GetInstance()->GetApkFile(), fileName, 0);
	if (!file) {
		PRINT("Error opening %s from APK", fileName);
		return;
	}

	//header for testing if it is a png
	png_byte header[8];

	//read the header
	zip_fread(file, header, 8);

	//test if png
	int is_png = !png_sig_cmp(header, 0, 8);
	if (!is_png) {
		zip_fclose(file);
		PRINT("Not is png");
	}

	//create png struct
	png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
		NULL, NULL);
	if (!png_ptr) {
		zip_fclose(file);
		PRINT("Cannot create png struct");
	}

	//create png info struct
	png_infop info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr) {
		png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
		zip_fclose(file);
		PRINT("Cannot create png info struct");
	}

	//create png info struct
	png_infop end_info = png_create_info_struct(png_ptr);
	if (!end_info) {
		png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
		zip_fclose(file);
		PRINT("Cannot create png info struct");
	}

	//png error stuff, not sure libpng man suggests this.
	if (setjmp(png_jmpbuf(png_ptr))) {
		zip_fclose(file);
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		PRINT("Cannot setjump");
	}

	//init png reading
	//png_init_io(png_ptr, f);
	png_set_read_fn(png_ptr, NULL, png_zip_read);

	//let libpng know you already read the first 8 bytes
	png_set_sig_bytes(png_ptr, 8);

	// read all the info up to the image data
	png_read_info(png_ptr, info_ptr);

	//variables to pass to get info
	int bit_depth, color_type;
	png_uint_32 twidth, theight;

	// get info about png
	png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
		NULL, NULL, NULL);

	//update width and height based on png info
	width = twidth;
	height = theight;

	// Update the png info struct.
	png_read_update_info(png_ptr, info_ptr);

	// Row size in bytes.
	int rowbytes = png_get_rowbytes(png_ptr, info_ptr);

	// Allocate the imageData as a big block, to be given to opengl
	//png_byte *imageData = new png_byte[rowbytes * height];
	imageData = new png_byte[rowbytes * height];
	if (!imageData) {
		//clean up memory and close stuff
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		zip_fclose(file);
		PRINT("Cannot create memory for image data");
	}

	//row_pointers is for pointing to imageData for reading the png with libpng
	png_bytep *row_pointers = new png_bytep[height];
	if (!row_pointers) {
		//clean up memory and close stuff
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		delete[] imageData;
		zip_fclose(file);
		PRINT("Cannot create memory for row pointer");
	}
	// set the individual row_pointers to point at the correct offsets of imageData
	for (int i = 0; i < height; ++i)
		row_pointers[height - 1 - i] = imageData + i * rowbytes;

	//read the png into imageData through row_pointers
	png_read_image(png_ptr, row_pointers);

	png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
	delete[] row_pointers;
	zip_fclose(file);
}
#endif

PngImage::~PngImage()
{
}